package org.cyhz.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 * excel工具类
 * @author sunla
 * @description
 * @time  2013-1-15 12:33:08
 * @email sunla
 * @version
 */
public class ExcelUtil {
    
    /**
     * 
    * @Description: workBook 
     *  @param fileName 
     * void
     */
    public static WritableWorkbook createWorkBook(String fileName){
        WritableWorkbook workBook=null;
        if(workBook==null){
            try {
                workBook=Workbook.createWorkbook(new File(fileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return workBook;
    }
    
    /**
     * 
    * @Description: sheet 
    *   @param workbook
     *  @param sheetName sheet
     *  @param number sheet
     *  @return 
     * WritableSheet
     */
    public static WritableSheet  createSheet(WritableWorkbook workbook,String sheetName,int number){
        return workbook.createSheet(sheetName, number);
    }
    
    /**
     * 
    * @Description: excel
     *  @param workbook
     *  @param fileName
     * void
     */
    public static void write(WritableWorkbook workbook){
        try{
            workbook.write();
            workbook.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public static Workbook getWorkbook(String path){
        Workbook work=null;
        try{
                work=Workbook.getWorkbook(new File(path));
        }catch (Exception e) {
        	e.printStackTrace();
            System.err.println(path+"路径不存在!");
     }
        return work;
    }
    
   /**
    * 
   * @Description: 
    *  @param work
    *  @param siteId
    *  @param sheetNum sheet
    *  @return 
    * List<Product>
    */
    public static void readExcel(Workbook work,int sheetNum){
        try{
            Sheet sheet=work.getSheet(sheetNum);
            int rowCount=sheet.getRows();
            int columCount=sheet.getColumns();
            for(int i=0;i<rowCount;i++){
                for(int j=0;j<columCount;j++){
                    /**getCell(int,int)  */
                    String str=sheet.getCell(j, i).getContents();
                   System.out.println(str);
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    /**
     * 
     * @Description: 读取xls并返回一个List对象 
     * @param sheet
     * @return
     * @return List<Map<String,String>>
     */
    public static List<Map<Integer,String>> readExcelForList(Sheet sheet){
    	List<Map<Integer,String>> list=new ArrayList<Map<Integer,String>>();
    	 int rowCount=sheet.getRows();
         int columCount=sheet.getColumns();
         for(int i=1;i<rowCount;i++){
        	 Map<Integer,String> valueMap=new HashMap<Integer,String>();
             for(int j=0;j<columCount;j++){
                 /**getCell(int,int)  */
                 String str=sheet.getCell(j, i).getContents();
                 valueMap.put(j, str);
                //System.out.println(str);
             }
             list.add(valueMap);
         }
    	return list;
    }
    
}
